package com.zzhou.linkinfo.clock.mvp.clockmain;

import com.zzhou.linkinfo.clock.entity.TaskBean;
import com.zzhou.linkinfo.clock.mvp.IBasicView;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;

/**
 * <desc>
 * Created by The Moss on 2019/3/13.
 */

public class MainPresenter extends IMainContract.APresenter {
    IMainContract.IModel iModel;
    IMainContract.IView iView;
    public MainPresenter(IBasicView iView) {
        super(iView);
        iModel = new MainModel();
        this.iView = (IMainContract.IView)iView;
    }

    @Override
    public void request() {
        Observable observable = Observable.create(new ObservableOnSubscribe<List<TaskBean>>() {
            @Override
            public void subscribe(ObservableEmitter<List<TaskBean>> e) throws Exception {
                iModel.request(new HashMap<String, Object>());
            }
        });
        observable.compose(iView.bindLifecycle())
                .subscribe(new Consumer<List<TaskBean>>() {
                    @Override
                    public void accept(List<TaskBean> o) throws Exception {
                        iView.onShowResult(true,initDatas());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        iView.onShowResult(false,null);
                    }
                });
    }
    private List<TaskBean> initDatas(){
        List<TaskBean> datas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            TaskBean bean = new TaskBean();
            bean.setTaskId(100+i);
            bean.setTaskTitle("发布打卡任务"+i);
            bean.setTaskContent("任务任务任务任务任务任务任务任务任务任务任务任务");
            bean.setTaskCreateTime(new Date().toString());
        }
        return datas;
    }
}
